package com.zzh.camera2.ui

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.ImageFormat
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Toast
import androidx.core.view.isVisible
import com.zzh.camera2.R
import com.zzh.camera2.manager.ThreadPoolManager
import com.zzh.camera2.model.CameraParameter
import com.zzh.camera2.permission.PermissionCheckUtil
import com.zzh.camera2.presenter.MainContract
import com.zzh.camera2.presenter.MainPresenter
import com.zzh.camera2.model.Const
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.parameter_fragment.*


class MainActivity : BaseActivity(), MainContract.View, View.OnClickListener {
    var presenter: MainContract.Presenter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // if permission no granted, then to grant permission view
        PermissionCheckUtil.requestAllPermissions(this)
//        transparentActionbar()
        initView()
        initPresenter()
    }

    private fun initView() {
        tvPreview1.isVisible = CameraParameter.isShowCallbackImage
        tvPreview2.isVisible = CameraParameter.isShowCallbackImage

        btnTakePhoto.setOnClickListener(this)
        btnTakePhoto2.setOnClickListener(this)
    }

    private fun initPresenter() {
        presenter = MainPresenter(this, this)
    }

    override fun onResume() {
        super.onResume()
        // camera 1
        presenter!!.openCamera(
            textureView,
            CameraParameter.aboveCameraId,
            Const.previewWidth,
            Const.previewHeight,
            ImageFormat.YUV_420_888
        )
        presenter!!.startPreview()
        // camera 2
        presenter!!.openCamera2(
            textureView2,
            CameraParameter.belowCameraId,
            Const.previewWidth,
            Const.previewHeight,
            ImageFormat.YUV_420_888
        )
        presenter!!.startPreview2()
    }

    override fun onPause() {
        super.onPause()
        // must be execute by thread, or ui jam
        // second opened camera, first to close.
        val command = Runnable {
            presenter!!.stopPreview()
            presenter!!.releaseCamera()

            presenter!!.stopPreview2()
            Handler(this.mainLooper).postDelayed(Runnable {
                presenter!!.releaseCamera2()
            }, 1)
        }
        ThreadPoolManager.getInstance().execute(command)
    }

    override fun setCameraApiVersion(apiVersion: String) {
        runOnUiThread {
            tvApiVersion.text = getString(R.string.api_version, apiVersion)
        }
    }

    override fun onPreviewCallback(bitmap: Bitmap?) {
        runOnUiThread {
            imgPreview.setImageBitmap(bitmap)
        }
    }

    override fun onPreviewCallback2(bitmap: Bitmap?) {
        runOnUiThread {
            imgPreview2.setImageBitmap(bitmap)
        }
    }

    override fun getAcContext(): Context {
        return this
    }

    override fun onClick(v: View?) {
        val id = v!!.id
        when (id) {
            R.id.btnTakePhoto -> {
                presenter!!.takePhoto()
            }
            R.id.btnTakePhoto2 -> {
                presenter!!.takePhoto2()
            }
        }
    }

    override fun showToast(content: String) {
        runOnUiThread {
            Toast.makeText(this, content, Toast.LENGTH_LONG).show()
        }
    }

    // 使状态栏透明
    private fun transparentActionbar() {
        //插入到OnCreate()中
        if (Build.VERSION.SDK_INT >= 21) {
            val window: Window = window
            window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
            window.getDecorView().setSystemUiVisibility(
                (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            )
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.setStatusBarColor(Color.TRANSPARENT)
            window.setNavigationBarColor(Color.TRANSPARENT)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ThreadPoolManager.getInstance().shutdown()
    }
}